/** @file MapLoader.qml
@brief The Qt-Complex special loader for the complex map object

The dynamic loader can be used to load the objects dinamically in the application
without any parameter specification. Using the load mechanism the objects are
customized with the theme settings when are loaded. The methods of the loader set the
essential theme properties authomatically.

The loader manages all the signal that can be connected with the objects. Depending by the
loaded object the presence of the corresponding signals.

@note The property object can be alternatively assigned using the the \e obj or
\e urlObj In the second case the property uses the Theme base url to find the object on
the network. \b Never set both the properties at the same time else the object can't be loaded.

@warning When the loader is instantiated to create an object inside its Item
it should be called \b objLoad Following an internal convention
all the objects expects the loader instantiated with this name.

@authorname \n
@licenseversion \n
@actualversion - @dateupdate

@licensenote

@todo Manage map signals to interact with map navigation from the application.
@todo Add map start position and location in the theme properties.
*/

import QtQuick 1.0

// The loader element surrounded by its item
Item {
    id: itemLoader

    // Loader properties to be assigned in the program
    property string obj: ""     // Object to load
    property string urlObj:  "" // Object loaded from a http url
    property string theme: ""   // Theme name
    // The following properties are used by the loaded

    // The used properties depends by the object type.
    property string obj_string: ""  // Generic string assigned to fields, buttons, etc.
    property string obj_message: ""  // Generic message assigne to text

    // Property values to contain route and location information
    // The location default position is preset as the program needs
    // but can be parametrized too.
    property string location: "Istanbul"
    property double lat: 41.0409106
    property double lon: 28.9861692
    property double zoom: 10

    // The signals connected with the object types
    signal clicked

    width: parent.width
    height: parent.height

    Loader {
        id:objLoad

        // Set of inline functions returning the requested property
        function getString() { return itemLoader.obj_string; }
        function getMessage() { return itemLoader.obj_message; }
        function getWidth() { return itemLoader.width; }
        function getHeight() { return itemLoader.height; }

        /**
      @fn getTheme()
      @brief Returns the theme name of the loader

      This function is used by the loaded object for the
      correct property assignement.
      @return theme Them theme property string
      */
        function getTheme() {
            return itemLoader.theme;
        }

        /**
      @fn objDestroy()
      @brief destroys the loaded object and free the associated resources.
      */
        function objDestroy() {
            source = "";
        }

        // The object source definition
        source: (itemLoader.obj == "") ? getBaseUrl() + itemLoader.urlObj + ".qml": itemLoader.obj + ".qml";

        // When loaded the map is set to the defaul starting location
        onLoaded: {
            objLoad.item.setLocation(itemLoader.lat, itemLoader.lon);
            objLoad.item.setMapZoom(itemLoader.zoom);
        }
    }

    // The connections to the possible signals returned by the objects
    // Actually maps does not manages external signals but the plant works.
    Connections {
        target: objLoad.item
        // The ignoerUnknownSignals is set to true because not all the loadable objects
        // manages the same signals.
        ignoreUnknownSignals: true
        // Signal returned by buttons and objects that manages buttonclick()
        onObjClicked: { itemLoader.clicked(); }
    }
}
